package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Panel;
import java.awt.Label;
import java.awt.Font;
import java.awt.Checkbox;
import javax.swing.JTable;
import javax.swing.JTextPane;
import java.awt.Button;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import Client.BusinessImplements;
import Controler.Extend_Item_Panel_Controler;
import Controler.Member_Card_Controler;
import DAL.Member_DAL;

import javax.swing.border.LineBorder;
/**
 * this panel handle with the extend of item by user and librerian (*reuse) 
 * @author Alex
 *
 */
public class Extend_Item_Panel extends Abstract_Jpanel {
	private JTable table;
	private JPanel panel;
	private JScrollPane scrollPane = new JScrollPane();
	public ArrayList<String> answer;
	private Object[][] data; 
	private String usedIdFromLibrerian = new String("");

	

	/**
	 * Create the panel.
	 */
	public Extend_Item_Panel() {
		setLayout(null);
		
		JButton btnExtend = new JButton("extend");
		btnExtend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				askForExtend();
			}

			
		});
		btnExtend.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnExtend.setBounds(576, 421, 106, 45);
		add(btnExtend);
		
		JButton btnBack = new JButton("Back");
		 btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		 btnBack.setBounds(88, 421, 98, 45);
		if(BusinessImplements.GetInstance().getUserLogin()!=null)
			 if (BusinessImplements.GetInstance().getUserLogin().getUserPosition()>=2)
			 add(btnBack);
		 
		 btnBack.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		BusinessImplements.GetInstance().getMainFrame().displayMenu();
		 		Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();

		 	}
		 });
		 
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(30, 44, 726, 366);
		add(panel);
		
		JLabel lblExtendItem = new JLabel("Extend Item");
		lblExtendItem.setForeground(new Color(25, 25, 112));
		lblExtendItem.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblExtendItem.setBounds(39, 11, 260, 22);
		add(lblExtendItem);
		

		initGUI();
	}
	private void initGUI() {
		setLayout(null);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(52, 30, 601, 298);
		panel.add(panel_1);
		panel_1.setLayout(null);
		scrollPane.setViewportBorder(new LineBorder(new Color(160, 160, 160)));
		scrollPane.setBounds(58, 33, 491, 233);
		panel_1.add(scrollPane);
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	/**
	 * method that shows the history of user by ID from librarian or user him self
	 */
	private void showHistory() {
		String userId = "";
		if (BusinessImplements.GetInstance().getUserLogin().getUserPosition() == 0 )
		{
			JOptionPane.showMessageDialog(null,"ERROR - You have to be registered member");
			return ;
		}
		if (BusinessImplements.GetInstance().getUserLogin().getUserPosition() == 1 )
			userId = Integer.toString( BusinessImplements.GetInstance().getUserLogin().getUserID() );
		if (BusinessImplements.GetInstance().getUserLogin().getUserPosition() > 1 )
			userId = usedIdFromLibrerian;
		String status = "2";
		Extend_Item_Panel_Controler eipc = new Extend_Item_Panel_Controler();
		answer = eipc.getLendHistory(userId);
		buildTable();
		
	}
	/**
	 * method that building table for showing the history of user ID
	 */
	private void buildTable() {
		String[] columnNames = {"Item Name","Item copy ID", "Lending date", "Return date"};
		String[] line;
		int S=answer.size();
		int x,r,siz=0;
		for (r=0; r<S; r++)
		{

			line = answer.get(r).split(",");
			if (line[4].equalsIgnoreCase("On lend"))
			siz++;

		}
		
		if (answer.get(0) == null)
			S=0;
	
		data = new Object[siz][4];
		x = 0; 
		for (r=0; r<S; r++)
		{

			line = answer.get(r).split(",");
			if (line[4].equalsIgnoreCase("On lend"))
			{
				data[x][0] = line[1];
				data[x][1] = line[2];
				data[x][2] = line[3];
				data[x][3] = line[5];
				x++;
			}

		}
		table = new JTable(data,columnNames);
		scrollPane.setViewportView(table);
		
	}
	/**
	 * method that trying to to check if it possible to make extend and make it
	 */
	private void askForExtend() {
		int r;
		String itemCopyid, librerianReturnDate;
		r=table.getSelectedRow();
		if (r == -1)
		{
			JOptionPane.showMessageDialog(null,"You have not selected anything");
			return ;
		}
		
		itemCopyid = (String) data[r][1];
		librerianReturnDate = (String) data[r][3];
		
		if (BusinessImplements.GetInstance().getUserLogin().getUserPosition() == 0 )
		{
			JOptionPane.showMessageDialog(null,"ERROR - You have to be registered member");
			return ;
		}
		
		String userId;
		
		if ( usedIdFromLibrerian.equals("") )
			if (BusinessImplements.GetInstance().getUserLogin().getUserPosition()==1)
				userId = Integer.toString( BusinessImplements.GetInstance().getUserLogin().getUserID() );
			else
			{
				JOptionPane.showMessageDialog(null,"ERROR - You haven't put User Id");
				return;
			}
		else 
			userId = usedIdFromLibrerian;
		String newDate;
		Extend_Item_Panel_Controler eipc = new Extend_Item_Panel_Controler();
		Member_Card_Controler mcc = new Member_Card_Controler();
		Member_DAL md;
		md = mcc.checkID( Integer.toString((BusinessImplements.GetInstance().getUserLogin().getUserID() ))) ;
		/**
		 * case user trying to extend for him self
		 */
		if  (BusinessImplements.GetInstance().getUserLogin().getUserPosition()==1) //user
		{
			if (md.getStatus().equalsIgnoreCase("0"))
			{	


				String res = eipc.userExtendAllowed(userId, itemCopyid).get(0);
				if (res.equals("OK"))
				{
					newDate = eipc.addNewUserExtend(userId, itemCopyid).get(0);
					JOptionPane.showMessageDialog(null,"Your new return date is :" +newDate);
					return;
				}
				else
				{
					if (res.equals("AlreadyOnOrderList"))
						JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed -  Already OnOrder List");
					else if (res.equals("itemIsWanted"))
						JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - itemIsWanted");
					else if (res.equals("null"))
						JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - item does not exists");
					else if (res.equals("NotYetWeek"))
						JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - it's more then a week to return");
						else  
							JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed");
					return;
				}
			}
			else JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - you are in FREEZE status");
			return;
		}
		/**
		 * case it librarian trying to make the extend for user
		 */
		else 
		{
			ArrayList<String> ans = eipc.librerianExtendAllowed(userId, itemCopyid);
			String res = ans.get(0);
			if (res.equals("OK"))
			{
				String librerianId = Integer.toString( BusinessImplements.GetInstance().getUserLogin().getUserID() );
				ans = eipc.addNewLibrerianExtend(userId, librerianId, itemCopyid, librerianReturnDate);
				res = ans.get(0);
				if (res.equals("OK"))
				{
					JOptionPane.showMessageDialog(null,"The Extend been made");
					return;
				}
				else if (res.equals("CanNotRetriveOldDate"))
				{
					JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - Item does not exists");
					return;
				}
				else if (res.equals("UnknownIsuue"))
				{
					JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - Unknown error occurred");
					return;
				}
				else if (res.equals("error"))
				{
					JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - ERROR");
					return;
				}
				else if (res.equals("librarianIdIsWrong"))
				{
					JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - ERROR");
					return;
				}
				return;
			}
			else if (res.equals("itemCopyNotExists"))
			{
				JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - Item Copy Not Exists");
				return;
			}
			else if (res.equals("memberNotExists"))
			{
				JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - Member Not Exists");
				return;
			}
			else if (res.equals("UnknownIsuue"))
			{
				JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - System Error");
				return;
			}
			else if (res.equals("error"))
			{
				JOptionPane.showMessageDialog(null,"ERROR - The Extend haven't been allowed - ERROR");
				return;
			}
			return;
		}
		
		

	}
	
	public void setUserId(String id){
		usedIdFromLibrerian = id;
	} 
	
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		showHistory();
	}
	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
}
